package com.solomon.travel.common.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import com.solomon.travel.common.custom.mapper.RestaurantCustomMapper;
import com.solomon.travel.common.custom.po.MenuCustom;
import com.solomon.travel.common.custom.po.RestaurantCustom;
import com.solomon.travel.common.mapper.ImageMapper;
import com.solomon.travel.common.mapper.ProtocalMapper;
import com.solomon.travel.common.mapper.RestaurantMapper;
import com.solomon.travel.common.model.Page;
import com.solomon.travel.common.po.Image;
import com.solomon.travel.common.po.ImageExample;
import com.solomon.travel.common.po.Menu;
import com.solomon.travel.common.po.Protocal;
import com.solomon.travel.common.po.ProtocalExample;
import com.solomon.travel.common.po.Restaurant;
import com.solomon.travel.common.po.RestaurantExample;
import com.solomon.travel.common.service.RestaurantService;
import com.solomon.travel.common.util.ConstantUtils;

@Service
public class RestaurantServiceImpl implements RestaurantService {

	@Override
	public int add(Restaurant restaurant) {
		return restaurantMapper.insertSelective(restaurant);
	}

	@Override
	public int update(Restaurant restaurant) {
		return restaurantMapper.updateByPrimaryKeySelective(restaurant);
	}

	@Override
	public int add(Protocal protocal) {
		return protocalMapper.insertSelective(protocal);
	}

	@Override
	public int update(Protocal protocal) {
		return protocalMapper.updateByPrimaryKeySelective(protocal);
	}

	@Override
	public int add(Menu menu) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int update(Menu menu) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Restaurant getByUser(String userId) {
		RestaurantExample example = new RestaurantExample();
		example.createCriteria().andUserIdEqualTo(userId);
		List<Restaurant> restaurants = restaurantMapper.selectByExample(example);
		if (!restaurants.isEmpty()) {
			return restaurants.get(0);
		}
		return null;
	}

	@Override
	public Restaurant getById(String objectId) {
		return restaurantMapper.selectByPrimaryKey(objectId);
	}

	@Override
	public Protocal getRestProtocal(String restId) {
		ProtocalExample example = new ProtocalExample();
		example.createCriteria().andOrganizationIdEqualTo(restId);
		List<Protocal> protocals = protocalMapper.selectByExample(example);
		if (!protocals.isEmpty()) {
			return protocals.get(0);
		}
		return null;
	}

	@Override
	public List<MenuCustom> getMenus(String restId) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Resource
	private ProtocalMapper protocalMapper;
	
	@Resource
	private RestaurantMapper restaurantMapper;
	

	@Override
	public RestaurantCustom getRestCustomByUser(String userId) {
		RestaurantExample example = new RestaurantExample();
		example.createCriteria().andUserIdEqualTo(userId);
		List<Restaurant> restaurants = restaurantMapper.selectByExample(example);
		if (!restaurants.isEmpty()) {
			RestaurantCustom restaurantCustom = new RestaurantCustom();
			Restaurant restaurant = restaurants.get(0);
			try {
				BeanUtils.copyProperties(restaurantCustom, restaurant);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			ImageExample imageExample = new ImageExample();
			imageExample.createCriteria().andOrganizationIdEqualTo(restaurant.getObjectId()).andSubtypeEqualTo(ConstantUtils.IMAGE_SUBTYPE_REST_INTRO);
			List<Image> restIntroImages = imageMapper.selectByExample(imageExample);
			restaurantCustom.setImages(restIntroImages);
			imageExample = new ImageExample();
			imageExample.createCriteria().andOrganizationIdEqualTo(restaurant.getObjectId()).andSubtypeEqualTo(ConstantUtils.IMAGE_SUBTYPE_REST_MENU);
			List<Image> restMenuImages = imageMapper.selectByExample(imageExample);
			restaurantCustom.setMenus(restMenuImages);
			ProtocalExample protocalExample = new ProtocalExample();
			protocalExample.createCriteria().andOrganizationIdEqualTo(restaurant.getObjectId());
			List<Protocal> protocals = protocalMapper.selectByExample(protocalExample);
			if (!protocals.isEmpty()) {
				restaurantCustom.setProtocal(protocals.get(0));
			}
			return restaurantCustom;
		}
		return null;
	}
	
	@Resource
	private ImageMapper imageMapper;

	@Resource
	private RestaurantCustomMapper restaurantCustomMapper;

	@Override
	public int count() {
		return restaurantMapper.countByExample(null);
	}

	@Override
	public List<Restaurant> listRest(Page page) {
		return restaurantCustomMapper.listByPage(page);
	}

	@Override
	public Integer countBySerch(Restaurant restaurant) {
		return restaurantCustomMapper.countBySerch(restaurant);
	}

	@Override
	public List<Restaurant> listBySerch(Restaurant restaurant, Page page) {
		return restaurantCustomMapper.listBySerch(restaurant, page);
	}

}
